/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} target
 * @return {number[][]}
 */
 var pathSum = function(root, target) {

    let res = [];

    let path = [];

    function getSum(root,sum){
        if(!root){
            return ;
        }
        if(root.val+sum == target && !root.left && !root.right){
            res.push([...path,root.val]);
            return ;
        }
        path.push(root.val);
        
        getSum(root.left,sum+root.val);
        getSum(root.right,sum+root.val);

        path.pop();
        
    }

    getSum(root,0);
    return res;
};